package com.yzc.controller;

import com.yzc.aliOSS.AliOSSUtils;
import com.yzc.result.Result;
import com.yzc.result.ResultCodeEnum;
import com.yzc.service.TourService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static com.yzc.result.ResultCodeEnum.*;

@RestController
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;

    @Autowired
    private TourService tourService;

    // 必须是不同的名字，不然只会识别第一个
    @PostMapping("/upload")
    public Result<String> upload(@RequestParam(value = "file", required = false) MultipartFile multipartFile,
                                     @RequestParam(value = "files", required = false) MultipartFile[] multipartFiles) throws Exception {
        if (multipartFile != null && !multipartFile.isEmpty()) {
            return handleSingleFileUpload(multipartFile);
        } else if (multipartFiles != null && multipartFiles.length > 0) {
            return handleMultipleFileUpload(multipartFiles);
        } else {
            return Result.fail(FILE_NOT_UPLOAD);
        }
    }

    private Result<String> handleSingleFileUpload(MultipartFile multipartFile) throws Exception {
        // 获取原文件名
        String originalFilename = multipartFile.getOriginalFilename();
        if (originalFilename == null) {
            return Result.fail(Invalid_FAIL);
        }
        // 保证文件名是唯一的
        String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));

        // 获取文件大小
        long size = multipartFile.getSize();
        if (size > 0) {
            // 上传单个文件
            String imgUrl = aliOSSUtils.uploadFile(multipartFile);
            return Result.success(imgUrl);
        } else {
            return Result.fail(FILE_SIZE_IS_ZERO);
        }
    }

    private Result<String> handleMultipleFileUpload(MultipartFile[] multipartFiles) throws Exception {
        List<MultipartFile> fileList = new ArrayList<>();
        for (MultipartFile file : multipartFiles) {
            if (file != null && !file.isEmpty()) {
                fileList.add(file);
            }
        }
        if (fileList.isEmpty()) {
            return Result.fail(Invalid_FAIL);
        }
        String urls = aliOSSUtils.uploadFiles(fileList);
        return Result.success(urls);
    }
    @GetMapping("/deleteOneFile")
    public Result<String> delOneFile(@RequestParam String url){
        return aliOSSUtils.deleteFile(url) ? Result.success("删除成功") : Result.fail(FILE_DELETE_ERROR);
    }

    @GetMapping("/deleteFile")
    public Result<String> delFile(@RequestParam Integer productId,@RequestParam("url") String url,@RequestParam String urls) {
        Boolean isDel = aliOSSUtils.deleteFile(url);
        if (!isDel) {
            return Result.fail(FILE_DELETE_ERROR);
        }
        // 删除数据库中地址
        String updateUrl = tourService.delProductUrl(productId,url,urls);
        return Result.success(updateUrl);
    }
}